from sakucrm import models
from stack.service.stack import StackConfig
from stack.service.stack import get_choice_text
from stack.service.stack import Option
from django import forms
from django.shortcuts import HttpResponse
from django.conf import settings
from django.db import transaction

class ConsultRecordConfig(StackConfig):

    list_display = ['customer','note','consultant']

    def get_queryset(self):
        cid=self.request.GET.get('cid')
        if cid:
            return models.ConsultRecord.objects.filter(customer_id=cid)
        else:
            return models.ConsultRecord.objects

class PriModelForm(forms.ModelForm):
    class Meta:
        model=models.ConsultRecord
        exclude=['customer','consultant']

class PriConsultRecordConfig(StackConfig):
    '''
    跟进记录
    '''

    list_display = ['customer', 'note', 'consultant']
    model_form_class = PriModelForm
    def get_queryset(self):
        current_user_id=1  #  以后这个数据要从session 中获取
        cid=self.request.GET.get('cid')
        if cid:
            return models.ConsultRecord.objects.filter(customer_id=cid,customer__consultant_id=current_user_id)
        else:
            return models.ConsultRecord.objects.filter(customer__consultant_id=current_user_id)

    def save(self,form,modify=False):
        if not modify:
            params=self.request.GET.get('_filter')
            cid,num=params.split('=',maxsplit=1)
            form.instance.customer=models.Customer.objects.get(id=num)  #获取客户信息
            current_user_id=1  # 这以后要从session中获取
            form.instance.consultant=models.UserInfo.objects.get(id=current_user_id)  #获取 销售信息
        form.save()